You work as an analyst at an investment fund in New York. Your CFO wants to explore if it is a good idea to invest some of the fund's assets in Bitcoin. You have to prepare a report on this asset and how it compares to the stock market in general.
CPI data from the U.S. Bureau of Labor Statistics. Publicly available information.
This project is module as followed:
prepared_data.pygraphing_functions.py hosted all the neccesary code to draw the graphsstock-terms.md responsible for the expert knowledge of the stock markets like indicators, formulas, ...import warnings
warnings.filterwarnings("ignore", category=DeprecationWarning)
from prepared_data import *
from graphing_functions import *
Bitcoin & SP500 ta chỉ quan tâm tới close price, vì đó là giá trị của 1 asset sau phiên giao dịchAnalyzed data based on pandas_profiling
bitcoin has 4 missing data on [2020-04-17, 2020-10-09, 2020-10-12, 2020-10-13]sp500 has 0 missing dataFor the sake of analysis, such missing data is dropped
draw_candle(sp500, 'S&P500 Candle Graph')
draw_candle(bitcoin, 'Bitcoin Candle Graph')
draw_line_plotly(monthly_data, 'date', 'gold_usd', 'Gold-USD price')
rank_by_btc.close_sp500.mean()
rank_by_btc.gold_usd.mean()
print(f'In average, Bitcoin surpass the price of SP500 by {rank_by_btc.close_sp500.mean():.2f}% and Gold by {rank_by_btc.gold_usd.mean():.2f}%')
import plotly.express as px
fig = px.line(rank_by_btc, x='date', y=['close_sp500', 'gold_usd'])
fig.show()
📊 Analysis 1:
Bitcoin có mức gia tăng khổng lồ so với các đối thủ - In average, Bitcoin surpass the price of SP500 by 295.04% and Gold by 605.84%, peaked in 2021 at 3400 lần so với vàng và 1400 lần so với SP500
Ta có thể thấy tính an toàn đến từ S&P500. Giá trị của SP500 mang tính tuần hoàn nhất định. Hầu hết các sóng giảm đều rơi vào khoảng đầu năm với khoảng thời gian từ 1-1.5 tháng, sau đó lại tăng mạnh. Notably nhất là lần rớt đỉnh khoảng đầu 2020 đã bốc hơi gần 1000 USD, tuy nhiên con sóng tăng tiếp theo đã đưa SP500 tăng 1 mạch 3000 USD, chạm mốc 4700 USD.
💡 Finding 1
In average, Bitcoin surpass the price of SP500 by 295.04% and Gold by 605.84%. Có thể thấy rằng khoảng thời điểm từ năm 2019 đến 2021 có rất nhiều sự biến động dẫn tới sự gia tăng đột biên này
draw_line_plotly(bitcoin, 'date', 'volume', 'Bitcoin trading volume')
draw_line_plotly(sp500, 'date', 'volume', 'S&P500 trading volume')
logy = LogYGraph((SeabornFn('lineplot', {'data': sp500_btc, 'x': sp500_btc.date, 'y': 'volume_sp500', 'color': 'red', 'label': 'S&P 500'}),
SeabornFn('lineplot', {'data': sp500_btc, 'x': sp500_btc.date, 'y': 'volume_btc', 'color': 'green', 'label': 'Bitcoin'}),
))
fig, ax1, ax2 = logy.draw()
fig.suptitle('Trading volume of Bitcoine vs S&P500')
ax1.set_title('Linear scale')
ax2.set_title('Log scale')
ax1.grid(axis='y')
ax2.grid(axis='y')
draw_line_plotly(bitcoin_obv, 'date', 'obv', 'Bitcoin OBV')
draw_line_plotly(sp500_obv, 'date', 'obv', 'SP500 OBV')
📊 Analysis 2:
Momentum về sự biến động giá theo volume giao dịch thể hiện rõ sự rớt giá này. Trong đầu đến giữa năm 2021, momentum của OBV giảm từ 2.5T -> 1.5T, dẫn tới Bitcoin rớt 20k USD
Observation về log-volume và price tương tự với S&P500. Ở S&P500, mặc dù ta thấy có 1 giai đoạn từ đầu đến giữa 2020 giá Stock-index giảm đáng kể, sự đi xuống của OBV ko đáng kể, và các mốc OBV tiếp theo toàn đi lên, indicates sự giảm đột ngột ko ảnh hưởng quá nhiều đến giá Stock nếu invested in long term
💡 Finding 2
Mặc dù sự quan tâm của nhà đầu tư có xu hướng tăng đáng kể qua từng năm, mức độ ổn định về giá so với độ quan tâm của loại tài sản Bitcoin có xu hướng ko ổn định, nhiều sự xuống giá đột ngột -> risky cho short-term investment so với S&P500 có tỉ số price/volume vô cùng ổn định và dễ đoán
logy = LogYGraph((SeabornFn('lineplot', {'data': std_gains, 'x': 'date', 'y': 'sp_std', 'color': 'red', 'label': 'S&P 500'}),
SeabornFn('lineplot', {'data': std_gains, 'x': 'date', 'y': 'btc_std', 'color': 'green', 'label': 'Bitcoin'}),
SeabornFn('lineplot', {'data': std_gains, 'x': 'date', 'y': 'gold_std', 'color': 'blue', 'label': 'Gold'}),
))
fig, ax1, ax2 = logy.draw()
fig.suptitle('Standardize return and Log return of assets')
ax1.set_title('Linear scale')
ax2.set_title('Log scale')
ax1.set_ylabel('return')
ax2.set_ylabel('return')
ax1.grid(axis='y')
ax2.grid(axis='y')
covariance_table
| sp_std | btc_std | gold_std | |
|---|---|---|---|
| sp_std | 1325.050180 | 1.329040e+05 | 651.253326 |
| btc_std | 132903.983795 | 1.626724e+07 | 60412.732951 |
| gold_std | 651.253326 | 6.041273e+04 | 453.607414 |
draw_broken_axis(data=std_year, column_as_x_axis='date', column_as_hue='assets', value_label='cummulate return', bot_lim=(-70, 20), top_lim=(150, 1000))
draw_kde_plot_of_dpc(dcp_df)
📊 Analysis 3:
DCP_Dist_Moments_Report(dcp_df[['sp500_dcp', 'btc_dcp', 'gold_dcp']])(95)
| sp500_dcp | btc_dcp | gold_dcp | |
|---|---|---|---|
| volatility ⬇ | 0.041 | 0.236 | 0.036 |
| kurtosis ⬇ | 1.642 | 0.069 | 0.305 |
| VaR (at 95) ⬆ | -0.065 | -0.282 | -0.053 |
| CVaR (at 95) ⬆ | -0.087 | -0.330 | -0.066 |
| MDD ⬆ | -0.200 | -0.764 | -0.161 |
CompareExtremeCVaR(dcp_df[['sp500_dcp', 'btc_dcp', 'gold_dcp']])()
| sp500_dcp | btc_dcp | gold_dcp | |
|---|---|---|---|
| CVaR at 80 | -0.048 | -0.204 | -0.042 |
| CVaR at 90 | -0.071 | -0.275 | -0.055 |
| CVaR at 95 | -0.087 | -0.330 | -0.066 |
| CVaR at 99 | -0.125 | -0.382 | -0.073 |
| PCT - 80 | -0.000 | -0.000 | -0.000 |
| PCT - 90 | 0.474 | 0.351 | 0.318 |
| PCT - 95 | 0.234 | 0.200 | 0.194 |
| PCT - 99 | 0.434 | 0.156 | 0.102 |
📊 Analysis 4:
💡 Finding 3
Return of Bitcoin follow the same trend as S&P500, but at much much more performant (illustrated in price chart, kurtosis parameter) and higher risk (illustrated in higher volatility, VaR, CVaR & MDD)
Risk_Return_Ratio_Report(dcp_df[['sp500_dcp', 'btc_dcp', 'gold_dcp']], dcp_df.sp500_dcp)()
| sp500_dcp | btc_dcp | gold_dcp | |
|---|---|---|---|
| Sharpe Ratio | 0.264 | 0.361 | 0.139 |
| Sortino Ratio | 0.338 | 0.842 | 0.250 |
| Information Ratio | nan | 0.328 | -0.100 |
| M2 Ratio | 0.011 | 0.015 | 0.006 |
📊 Analysis 5:
💡 Finding 4
Bitcoin outperform S&P500 and Gold in every Risk-Return categories
sns.lineplot(data=inflation_year, x='date', y='inflation')
plt.axhline(y=inflation_rate)
<matplotlib.lines.Line2D at 0x1bd4271ab50>
slope_rp
| Inflation vs S&P500 | Inflation vs Bitcoin | Inflation vs Gold | |
|---|---|---|---|
| Slope | 4.149077 | 12.416423 | -1.108720 |
| R-squared | 0.591565 | 0.046890 | -0.155191 |
fig, (ax1, ax2, ax3, ax4) = plt.subplots(1, 4, figsize=(20, 5), sharey=True)
draw_ecdf(ax1, inflation_month.inflation, 'Inflation')
draw_ecdf(ax2, inflation_month.btc_pct, 'Bitcoin')
draw_ecdf(ax3, inflation_month.sp_pct, 'S&P500')
draw_ecdf(ax4, inflation_month.gold_pct, 'Gold')
fig, ax = draw_compare_boxplot([extreme_btc, middle_btc, lowest_btc], ['High CPI (top 10%)','Medium CPI','Low CPI (bottom 10%)'])
plt.title('Monthly Bitcoin returns by inflation percentiles')
ax.set_ylabel('Bitcoin returns')
print('Triangle is Mean, Line is Median \n')
plt.show()
Triangle is Mean, Line is Median
fig, ax = draw_compare_boxplot([extreme_sp, middle_sp, lowest_sp], ['High CPI (top 10%)','Medium CPI','Low CPI (bottom 10%)'])
plt.title('Monthly S&P500 returns by inflation percentiles')
ax.set_ylabel('SP500 returns')
print('Triangle is Mean, Line is Median \n')
plt.show()
Triangle is Mean, Line is Median
fig, ax = draw_compare_boxplot([extreme_gold, middle_gold, lowest_gold], ['High CPI (top 10%)','Medium CPI','Low CPI (bottom 10%)'])
plt.title('Monthly Gold returns by inflation percentiles')
ax.set_ylabel('Gold returns')
print('Triangle is Mean, Line is Median \n')
plt.show()
Triangle is Mean, Line is Median
💡 Finding 5
Bitcoin is an independent asset comparing to inflation, so it is a good hedge against inflation or not is related to the risk-willingness the coporate would spend (best for high risk - high reward strategy)
With portfolio pct_change, we can easily calculate: Volatility, Sharpe ratio, Sortino Ratio, VaR & CVaR
display(tt.create_volatility_rp('Bitcoin', 'SP500'),
tt.create_ratio_rp(sharpe_ratio, 'bitcoin', 'sp500', 'Sharpe'),
tt.create_ratio_rp(sortino_ratio, 'bitcoin', 'sp500', 'Sortino'),
tt.create_var_rp(value_at_risk_single, 90, 'bitcoin', 'sp500', 'VaR'),
tt.create_var_rp(expected_shortfall, 90, 'bitcoin', 'sp500', 'CVaR'),
)
| 20% Bitcoin | 40% Bitcoin | 60% Bitcoin | 80% Bitcoin | |
|---|---|---|---|---|
| 20% SP500 | 0.053881 | 0.097706 | 0.144056 | 0.191102 |
| 40% SP500 | 0.055376 | 0.100339 | 0.147077 | nan |
| 60% SP500 | 0.059181 | 0.104221 | nan | nan |
| 80% SP500 | 0.064891 | nan | nan | nan |
| 20% bitcoin | 40% bitcoin | 60% bitcoin | 80% bitcoin | |
|---|---|---|---|---|
| 20% sp500 | 0.411798 | 0.391230 | 0.376680 | 0.367868 |
| 40% sp500 | 0.421765 | 0.392600 | 0.376880 | nan |
| 60% sp500 | 0.414377 | 0.389180 | nan | nan |
| 80% sp500 | 0.395907 | nan | nan | nan |
| 20% bitcoin | 40% bitcoin | 60% bitcoin | 80% bitcoin | |
|---|---|---|---|---|
| 20% sp500 | 1.096583 | 0.954729 | 0.900577 | 0.854240 |
| 40% sp500 | 1.024867 | 0.968305 | 0.859560 | nan |
| 60% sp500 | 0.877333 | 0.881725 | nan | nan |
| 80% sp500 | 0.730776 | nan | nan | nan |
| 20% bitcoin | 40% bitcoin | 60% bitcoin | 80% bitcoin | |
|---|---|---|---|---|
| 20% sp500 | -0.043533 | -0.067770 | -0.102013 | -0.139887 |
| 40% sp500 | -0.043704 | -0.066070 | -0.102028 | nan |
| 60% sp500 | -0.049801 | -0.078533 | nan | nan |
| 80% sp500 | -0.060850 | nan | nan | nan |
| 20% bitcoin | 40% bitcoin | 60% bitcoin | 80% bitcoin | |
|---|---|---|---|---|
| 20% sp500 | -0.052896 | -0.107739 | -0.164794 | -0.223165 |
| 40% sp500 | -0.059158 | -0.113211 | -0.171023 | nan |
| 60% sp500 | -0.071098 | -0.122467 | nan | nan |
| 80% sp500 | -0.086406 | nan | nan | nan |
efficient = SimulateEfficientFrontier(portfolio_data, 1000)()
DE = DrawEfficientFrontier(efficient, 'volatility', 'mean', 'sortino ratio', 'index_col')
DE('Annualised Risk (Volatility)', 'Annualised Return').show()
best_ratio = DE.get_max_sortino()['weight seed'].values[0]
print(f'Best ratio is Bitcoin: {best_ratio[0]*100:.1f}%, Stock market: {best_ratio[1]*100:.1f}%, Gold: {best_ratio[2]*100:.1f}%')
Best ratio is Bitcoin: 15.3%, Stock market: 20.6%, Gold: 64.1%
💡 Finding 6
Best ratio is Bitcoin: 15.3%, Stock market: 20.6%, Gold: 64.1%
💡 Finding 1
In average, Bitcoin surpass the price of SP500 by 295.04% and Gold by 605.84%. Có thể thấy rằng khoảng thời điểm từ năm 2019 đến 2021 có rất nhiều sự biến động dẫn tới sự gia tăng đột biên này💡 Finding 3
Return of Bitcoin follow the same trend as S&P500, but at much much more performant (illustrated in price chart, kurtosis parameter) and higher risk (illustrated in higher volatility, VaR, CVaR & MDD)
💡 Finding 2
Mặc dù sự quan tâm của nhà đầu tư có xu hướng tăng đáng kể qua từng năm, mức độ ổn định về giá so với độ quan tâm của loại tài sản Bitcoin có xu hướng ko ổn định, nhiều sự xuống giá đột ngột -> risky cho short-term investment so với S&P500 có tỉ số price/volume vô cùng ổn định và dễ đoán💡 Finding 4
Bitcoin outperform S&P500 and Gold in every Risk-Return categories💡 Finding 5
Bitcoin is an independent asset comparing to inflation, so it is a good hedge against inflation or not is related to the risk-willingness the coporate would spend (best for high risk - high reward strategy)
💡 Finding 6
Best ratio is Bitcoin: 15.3%, Stock market: 20.6%, Gold: 64.1%